package com.glp.work.flow.exception;

import org.springframework.util.StringUtils;

import com.glp.common.reponse.BaseResponse;
import com.glp.common.reponse.BaseResult;
import com.glp.work.flow.constant.APICode;

public class APIException extends RuntimeException {

  private static final long serialVersionUID = 8907245640919300900L;

  private APICode apiCode;
  private String message;

  public APIException() {
    super();
  }

  public APIException(APICode apiCode) {
    super();
    this.apiCode = apiCode;
    if (apiCode != null)
      this.message = apiCode.getMessage();
  }

  public APIException(APICode apiCode, String message) {
    super();
    this.apiCode = apiCode;
    this.message = message;
  }

  public BaseResult parse() {

    BaseResult response=BaseResponse.success();
    response.setCode(this.apiCode.getCode());

    if (StringUtils.hasText(message)) {
      response.setMessage(message);
    }
    return response;
  }

  public APICode getApiCode() {
    return apiCode;
  }

  public void setApiCode(APICode apiCode) {
    this.apiCode = apiCode;
  }

  @Override
  public String getMessage() {
    return message;
  }

  public void setMessage(String message) {
    this.message = message;
  }

}
